<?php
	class Weixin_notice extends MY_Controller {
		function class_begin(){
			$this->load->model("Class_plan");
			$plan_list = $this->Class_plan->get_tomorrow_class_plan_user();
			if($plan_list){
				$user_list = $class_id = array();
				foreach ($plan_list as $key => $value) {
					$plan_data = array("class_time" => !empty($value['class_time_str']) ? $value['class_date'].$value['class_time_str'] : $value['class_time'], "school_id" => $value['school_id']);
					$user_list[$value['id']]['user_list'] = $this->Class_plan->get_classes_user_openid($value['class_id']);
					$user_list[$value['id']]['plan_data'] = $plan_data;
					/*if(!empty($value['class_id']) && !in_array($value['class_id'], $class_id)){
						$class_id[] = $value['class_id'];
						$user_list[] = $this->Class_plan->get_classes_user_openid($value['class_id']);
					}*/
				}
				if($user_list){
					$notice_data = array();
					foreach ($user_list as $k => $v) {
						foreach ($v['user_list'] as $k1 => $v1) {
							$data = array(
										"first" 	=> array("value" => '亲爱的同学，您有新的上课提醒，请查阅。'),
										"keyword1" 	=> array("value" => $v1['course_name']),
										"keyword2"	=> array("value" => $v['plan_data']['class_time']),
										"keyword3"  => array("value" => $v1['name'] . "【{$v1['class_name']}】"),
										"remark"	=> array("value" => '请提前做好课程预习，提前15分钟到达上课地点。')
									);
							$this->wx_notice($v1['open_id'],  $data, $v['plan_data']['school_id']);
						}
					}
				}
				/*if($class_id){
					$class_id = implode(",", $class_id);
					$user_list = $this->Class_plan->get_classes_user_openid($class_id);
					print_r($user_list);
				}*/
			}
		}

		function wx_notice($touser, $data, $school_id){
			require_once "third_party/wxjsapi/wxapi_utils.php";
			$wxapi_sccesstoken = new Wxapi_Utils($school_id);
			$sccesstoken = $wxapi_sccesstoken->getAccessToken();
			$req_url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=" . $sccesstoken;
			$wx_data = array(
							"touser" => $touser,
							"template_id" => "rSfdjvhww2EOeOlk_cG_Pe_Iaxc3IzaAQVv_1oKqdko",
							// "url" => "http://test.deanxin.com",
							"data" => $data
						);
			$result = curlRequest($req_url, $wx_data, "POST", TRUE);
			var_dump($result);
			return $result;
		}
	}